classdef MultiPacket < handle
    %MULTIPACKET Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        id = 0;
        id_history = [];
        id_history_index = 1;
        packet;
        packet_id_range_min = 0;
        packet_id_range_max = 0;
        num_of_packets = 0;
        % this packet also contains inforamtion for the link layer
        channel_index = 0;
    end
    
    properties(Constant)
        max_history_id_length = 50;
    end
    
    methods
        function obj=MultiPacket(id, packet,min_id_range,max_id_range)
            if nargin>0
                obj.id_history(obj.max_history_id_length) = 0;
                obj.id = id;
                obj.packet = packet;
                obj.packet_id_range_min = min_id_range;
                obj.packet_id_range_max = max_id_range;
            end
        end
        
        function set.id(obj, value)
            obj.id = value;
            
            assert(obj.id_history_index < obj.max_history_id_length);
            obj.id_history(obj.id_history_index) = value;
            obj.id_history_index = obj.id_history_index + 1;
        end
        
        function set.packet_id_range_min(obj,value)
            obj.packet_id_range_min = value;
            obj.num_of_packets = obj.packet_id_range_max - obj.packet_id_range_min +1;
        end
        
        function set.packet_id_range_max(obj,value)
            obj.packet_id_range_max = value;
            obj.num_of_packets = obj.packet_id_range_max - obj.packet_id_range_min +1;
        end
        
        function new_multi_packet = split_multi_packet(obj,new_min_packet_id)
            packets_manager = PacketsManager.instance();
            
            new_multi_packet_id = packets_manager.get_next_multi_packet_id();
            new_packet = obj.packet.duplicate_packet();
            new_multi_packet = MultiPacket(new_multi_packet_id, new_packet,obj.packet_id_range_min, new_min_packet_id-1);
            new_multi_packet.id_history = obj.id_history;
            new_multi_packet.id_history_index = obj.id_history_index;
            new_multi_packet.id = new_multi_packet.id;
            new_multi_packet.channel_index = obj.channel_index;
            
            %set current multi packet
            obj.packet_id_range_min = new_min_packet_id;
            %set a new id to the current packet (beacuse of the split)
            new_multi_packet_id_for_this_multi_packet = packets_manager.get_next_multi_packet_id();
            obj.id = new_multi_packet_id_for_this_multi_packet;
        end
        
        function new_multi_packet = duplicate_multi_packet(obj)
            new_packet = obj.packet.duplicate_packet();
            new_multi_packet = MultiPacket(obj.id, new_packet, obj.packet_id_range_min,obj.packet_id_range_max);
            new_multi_packet.channel_index = obj.channel_index;
            new_multi_packet.id_history = obj.id_history;
            new_multi_packet.id_history_index = obj.id_history_index;
        end
    end
    
end

